<template>
  <div class="contain">
    <el-card>
      <div class="top">
        <el-button
          type="primary"
          size="small"
          @click="editShowDialog = true"
        >添加角色</el-button>
      </div>
      <el-table
        :data="roleList"
        row-key="id"
        border
        default-expand-all
        style="width: 100%"
      >
        <el-table-column prop="title" label="角色名称" />
        <el-table-column prop="description" label="角色描述" />
        <el-table-column prop="address" label="操作">
          <template v-slot="{ row }">
            <el-button
              size="small"
              type="primary"
              @click="assigning(row.id)"
            >分配权限</el-button>
            <el-button size="small" @click="edit(row.id)">编辑</el-button>
            <el-button
              size="small"
              type="danger"
              @click="delRole(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="prev, pager, next"
        :page-size="pageset._limit"
        :total="total"
        :page-count="pageCount"
        :current-page="currentPage"
        @current-change="currentChange"
      />
    </el-card>

    <el-dialog
      title="分配权限"
      :visible="showDialog"
      width="50%"
      :before-close="handleClose"
    >
      <el-tree
        ref="tree"
        :data="permissionList"
        show-checkbox
        default-expand-all
        node-key="id"
        highlight-current
        :default-checked-keys="defaultCheck"
        :props="defaultProps"
      />

      <template #footer>
        <el-button type="primary" @click="btnOk">确认</el-button>
        <el-button @click="handleClose">取消</el-button>
      </template>
    </el-dialog>

    <editDialog
      ref="editDialog"
      :edit-show-dialog.sync="editShowDialog"
      @getRoles="getRoles"
    />

  </div>
</template>

<script>
import editDialog from './component/editDialog.vue'
import { getRoles, delRole, getRoleById, editRole, getCount } from '@/api/role'
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils/index'
export default {
  components: {
    editDialog
  },
  data() {
    return {
      roleList: [],
      roleId: '',
      editShowDialog: false,
      showDialog: false,
      permissionList: [],
      defaultCheck: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      total: 0,
      currentPage: 1,
      pageset: {
        _limit: 5,
        _start: 0
      }
    }
  },
  computed: {
    pageCount() {
      if (this.total % this.pageset._limit === 0) {
        return parseInt(this.total / this.pageset._limit)
      } else {
        return parseInt(this.total / this.pageset._limit) + 1
      }
    }
  },
  created() {
    this.getRoles()
    this.getCount()
  },
  methods: {
    async getCount() {
      const { data } = await getCount()
      this.total = data
    },
    async getRoles(type) {
      // 添加角色时需要跳到最后一页
      if (type === 'add') {
        this.total % this.pageset._limit === 0 ? this.currentChange(this.pageCount + 1)
          : this.currentChange(this.pageCount)
      } else {
        const { data } = await getRoles(this.pageset)
        this.roleList = data
        this.getCount()
      }
    },
    async edit(id) {
      await this.$refs.editDialog.getRoleById(id)
      this.editShowDialog = true
    },
    async assigning(id) {
      const { data } = await getPermissionList()
      const res = await getRoleById(id)
      this.roleId = id
      this.defaultCheck = res.data.permissions?.map(item => item.id)
      this.permissionList = listToTreeData(data, '0')
      this.showDialog = true
    },
    async delRole(id) {
      try {
        await this.$confirm('确定删除？')
        await delRole(id)

        if (this.total % this.pageset._limit === 1) {
          await this.currentChange(this.pageCount - 1)
        } else {
          await this.getRoles()
          await this.getCount()
        }
        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    },
    async btnOk() {
      await editRole({
        'id': this.roleId,
        'permissions': this.$refs.tree.getCheckedNodes()
      })
      this.$message.success('操作成功')
      this.getRoles()
      this.handleClose()
    },
    async currentChange(index) {
      this.pageset._start = this.pageset._limit * (index - 1)
      await this.getRoles()
      await this.getCount()
      this.currentPage = index
    },
    handleClose() {
      this.showDialog = false
      this.defaultCheck = []
      this.roleId = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.contain {
  padding: 10px;
  .top {
    margin: 0 10px;
    padding: 10px 0;
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
}
</style>
